package com.atguigu.bigdata.spark.sql

import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

/**
  * create by undeRdoG on  2021-06-17  23:25
  * 凡心所向，素履以往，生如逆旅，一苇以航。
  */
object Spark02_SparkSQL_UDF {

  def main(args: Array[String]): Unit = {

    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkSQL")

    val spark = SparkSession.builder().config(sparkConf).getOrCreate()
    import spark.implicits._


    val df: DataFrame = spark.read.json("datas/user.json")

    df.createOrReplaceTempView("user")

    /**
    *  自定义 udf
    * */
    spark.udf.register("prefix",(name:String) => {
      "Name:" + name
    })

    spark.sql("select age, prefix(username) from user").show()

  }


  case class User(id: Int,name:String,age:Int)
}
